/**
 * 年返奖励报表
 * @author shixc
 * @date 2019/05/08
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table, END,START,
        table_gwda = {},
        cols = [[
            {field: 'ID', type: 'checkbox', title: '选择', fixed: 'left'}
            , {field: 'GWDD012', width: 120, title: '客户编号', fixed: 'left', sort: true}
            , {field: 'GWEB003', width: 80, title: '客户名称'}
            , {field: 'GWADL004', width: 120, title: '省份'}
            , {field: 'MA041', width: 80, title: '结算方式'}
            , {field: 'GWDE004', width: 120, title: '返利编号'}
            , {field: 'GWAHL003', width: 80, title: '系列'}
            , {field: 'FH', width: 80, title: '发货金额'}
            , {field: 'TH', width: 80, title: '退货金额'}
            , {field: 'WFLFH', width: 80, title: '无返利发货'}
            , {field: 'PHFH', width: 80, title: '铺货发货'}
            , {field: 'PHHK', width: 80, title: '铺货回款'}
            , {field: 'PHWFL', width: 80, title: '铺货无返利回款'}
            , {field: 'CPHJ', width: 80, title: '不计入产品指标合计'}
            , {field: 'CPMX', width: 80, title: '不计入产品指标明细'}
            , {field: 'TZHJ', width: 80, title: '系统外调整合计'}
            , {field: 'TZMX', width: 80, title: '系统外调整明细'}
            , {field: 'ZBJE', width: 80, title: '指标金额'}
            , {field: 'GWDD019', width: 80, title: '指标比率'}
            , {field: 'YJFH', width: 80, title: '应记发货金额'}
            , {field: 'FL', width: 80, title: '返利金额'}
        ]],
        gwai780SettingAction, gwai780table, company
    ;

    var Gwai800 = app.Module.extend({
        init: function () {
            self = this;
            var _searchsArr = app.cloneJSON(cols[0]);
            var date = new Date();
            END = date.Format("yyyy-MM-dd");
            date.setMonth(0);
            date.setDate(1);
            START = date.Format("yyyy-MM-dd");
            var datas = [];
            app.ajax({
                xurl: 'crm/gwai800/getGwdeData',
                async: false,
                cb: function (result) {
                    if (result.ok) {
                        datas = result.data.data;
                        datas = datas.concat({code: '', value: '全部'});
                    }
                }
            });
            self._super({
                _tag: 'gwai800',
                _searchs: [{
                    id: 'gwai800', tableId: 'gwai800_table', datas: _searchsArr.pushArray([
                        {
                            field: 'COMPANY',
                            title: '法人组织',
                            search: 'default',
                            searchOpts: {icon: true}
                        },
                        {
                            field: 'GWDD012',
                            title: '经销商',
                            search: 'default',
                            searchOpts: {icon: true}
                        },
                        {
                            field: 'GWDD017',
                            title: '返利编号',
                            search: 'default',
                            filterDatas: datas,
                            searchOpts: {type: 'select'}
                        }
                        , {
                            field: 'DATERANGE',
                            title: '日期范围',
                            search: 'default',
                            searchOpts: {type: 'daterange', default: [START, END]}
                        }
                    ])
                }],
                _loadTplsFull: [ {
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                gwai780table = table.render({
                    elem: app.getByid(doc, 'gwai800_table'),
                    url: g_ctxPath + 'crm/gwai800/getList',
                    title: '对账确认单信息表',
                    method: 'post',
                    id: 'gwai800_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'], //, 'exports_import'
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_gwda,
                    done: function (res, curr, count) {

                    }
                });

                var tipsIndex = null;

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwai800_table', where: table_gwda});
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwai800',
                    methods: {
                        handleIconClick: function (field) {
                            var vue = this;
                            if (field == 'GWDD012') {//客户开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method:'q_gwak001',
                                            columnList: [
                                                {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                {label: '客户全称', prop: 'GWAKL004', width: '150',sortable:false,unshow:true}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.GWDD012 = data.GWAK001;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择经销商',
                                        area: ['600px', '465px']
                                    }
                                });
                            } else if (field == 'COMPANY') {//法人开窗
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        config: {
                                            method: 'q_gwde003',
                                            searchParam:{
                                                GWDE002 : "ERP账套"
                                            },
                                            columnList: [
                                                {label: '选项值', prop: 'GWDE003', width: '150'},
                                                {label: '选项名称', prop: 'GWDE004', width: '150'}
                                            ],
                                        },
                                        cb: function (data) {
                                            vue.isearch.fields.COMPANY = data.GWDE003;
                                            company = data.GWDE003;
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择法人',
                                        area: ['600px', '470px']
                                    }
                                });
                            }
                        }
                    },
                    watch: {
                        'isearch.fields.DATERANGE': function (newValue, oldValue) {
                            if (newValue != null && newValue.length == 2) {
                                var arr1 = newValue[0].substring(0,4);
                                var arr2 = newValue[1].substring(0,4);
                                if(arr1 != arr2){
                                    app.showWarning("日期范围不能跨年！");
                                    this.isearch.fields.START = START;
                                    this.isearch.fields.END = END;
                                    this.isearch.fields.DATERANGE = [START,END];
                                    return;
                                }else {
                                    this.isearch.fields.START = newValue[0].replace(/-/g, '');
                                    this.isearch.fields.END = newValue[1].replace(/-/g, '');
                                }
                            } else {
                                this.isearch.fields.START = '';
                                this.isearch.fields.END = '';
                            }
                        },
                        'isearch.fields.COMPANY': function (val) {
                            if (val == '') {
                                company = '';
                            }else {
                                company = val;
                            }
                        }
                    }
                }]
            });
            table_gwda.END = END;
            table_gwda.START = START;
            self.initTable();
            $('#itoSearch').click(function () {
                if (company == null || company == '') {
                    app.showWarning("法人必选！");
                    return;
                }
            })
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

    return new Gwai800();

});